package com.bluegalaxy.nhb.snservice.common.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;

import javax.sql.DataSource;
import javax.annotation.Resource;

@Configuration
//@tk.mybatis.spring.annotation.MapperScan(basePackages = "com.bluegalaxy.nhb.snservice.dao")
//@MapperScan(basePackages = "com.bluegalaxy.nhb.snservice.dao", sqlSessionTemplateRef = "clickHouseSqlSessionFactoryBean")
public class DruidConfig {
    @Resource
    private JdbcParamConfig jdbcParamConfig;
//
    @Bean
    public DataSource clickhouseDataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(jdbcParamConfig.getUrl());
        datasource.setDriverClassName(jdbcParamConfig.getDriverClassName());
        datasource.setInitialSize(jdbcParamConfig.getInitialSize());
        datasource.setMinIdle(jdbcParamConfig.getMinIdle());
        datasource.setMaxActive(jdbcParamConfig.getMaxActive());
        datasource.setMaxWait(jdbcParamConfig.getMaxWait());
        datasource.setUsername(jdbcParamConfig.getUsername());
        datasource.setPassword(jdbcParamConfig.getPassword());

//        datasource.setValidationQuery("select 1");
//        datasource.setTestOnBorrow(true);
//        datasource.setTestOnReturn(true);
//        datasource.setTestWhileIdle(true);
//        datasource.setTimeBetweenEvictionRunsMillis(15000);
//        datasource.setMinEvictableIdleTimeMillis(60000);
//        datasource.setRemoveAbandoned(true);
//        datasource.setRemoveAbandonedTimeout(3600);
//        datasource.setLogAbandoned(true);
        return datasource;
    }

    // 注入数据源配置
//    @Bean
//    public DataSource clickhouseDataSource() throws Exception {
//        Class classes = Class.forName("com.alibaba.druid.pool.DruidDataSource");
//        DruidDataSource dataSource = (DruidDataSource) DataSourceBuilder
//                .create()
//                .driverClassName(jdbcParamConfig.getDriverClassName())
//                .type(classes)
//                .url(jdbcParamConfig.getUrl())
//                .username(jdbcParamConfig.getUsername())
//                .password(jdbcParamConfig.getPassword())
//                .build();
//        dataSource.setMaxWait(jdbcParamConfig.getMaxWait());
//        dataSource.setValidationQuery("SELECT 1");
//        return dataSource;
//    }
//
//    @Bean
//    public SqlSessionFactory clickHouseSqlSessionFactoryBean() throws Exception {
//        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
//        factory.setDataSource(clickhouseDataSource());
//        // 实体 model的 路径 比如 com.order.model
//        factory.setTypeAliasesPackage("com.bluegalaxy.nhb.snservice.domain.model");
//        //添加XML目录
//        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
//        //开启驼峰命名转换
//        factory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
//        return factory.getObject();
//    }



}
